<template>
  <div>
    <div class="slide-panel-mask" v-bind:style="{display:upload ? '' : 'none'}"></div>
    <div class="side-panel" v-bind:style="{right:upload ? '0' : '-100%'}">
      <div class="panel-title">上传选项 <i class="el-icon-close" @click="close"></i></div>
      <div class="panel-body">
        <div class="panel-content">
          <form action="">
            <div class="form-row">
              <label for="">文件目录：</label>
              <div class="form-field">
                <div>
                  <input type="button" class="btn-normal" v-bind:class="{'btn-light':updir==1}"
                         @click="changeType(1)"
                         value="当前目录">
                  <input class="btn-normal" type="button" v-bind:class="{'btn-light':updir==2}" @click="changeType(2)"
                         value="指定目录">
                </div>
              </div>
            </div>
            <div class="form-row">
              <label for="">目录地址：</label>
              <div class="form-field">
              <span v-if="updir==1">
              ./</span>
                <input v-if="updir==2" @keyup="handlekeyUp" v-model="dirName" class="form-control">
              </div>
            </div>
            <div class="form-row"><label for="">文件 ACL</label>
              <div class="form-field">
                <input type="button" class="btn-normal" v-bind:class="{'btn-light':acl==4}" @click="acl=4"
                       value="继承Bucket">
                <input type="button" class="btn-normal" v-bind:class="{'btn-light':acl==1}" @click="acl=1" value="私有">
                <input type="button" class="btn-normal" v-bind:class="{'btn-light':acl==2}" @click="acl=2" value="公共读">
                <input type="button" class="btn-normal" v-bind:class="{'btn-light':acl==3}" @click="acl=3" value="公共读写">
              </div>
            </div>
            <div class="form-row"><label for="">上传 </label>
              <div class="form-field">

                <el-upload
                  class="upload-demo"
                  ref="upload" multiple
                  action="/api/object/putObject"
                  :on-preview="handlePreview"
                  :on-remove="handleRemove"
                  :file-list="fileList" :data="parameters" :onSuccess="handleUp" :before-upload="uploadBefore"
                  :onError="error"
                  :auto-upload="false">
                  <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                  <el-button class="upsubmit" size="small" type="success" :loading="loading" @click="submitUpload">
                    上传到服务器
                  </el-button>
                </el-upload>
                <div class="info">
                  <p> 文件的命名规范如下：</p>
                  <p>- 使用 UTF-8 编码</p>
                  <p>- 请输入正确的目录地址，仅包括：中文汉字、字母、数字、-、_、/</p>
                </div>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

</template>
<script>
  export default {
    name: 'upload',
    data () {
      return {
        updir: 1,
        acl: 4,
        dirName: '',
        loading: false,
        parameters: {
          bucketId: this.bucketId,
          path: this.dirName,
          isForce: 1,
          uploadType: this.updir,
          parentObjectId: this.parentId,
          objectAcl: this.acl
        },
        fileList: [],
        submitCount: 0,
        fileCount: ''
      }
    },
    props: ['upload', 'bucketId', 'parentId'],
    watch: {},
    updated () {
      this.parameters.bucketId = this.bucketId
      this.parameters.parentObjectId = this.parentId
    },
    methods: {
      changeType (type) {
        this.updir = type
        this.parameters['uploadType'] = type
        if (this.updir === 2) {
          delete this.parameters.parentObjectId
        }
      },
      close () {
        this.$refs.upload.clearFiles()
        this.dirName = ''
        this.submitCount = 0
        this.fileCount = ''
        this.$emit('showbox', false)
      },
      upprogress () {
        console.log(1)
      },
      handlekeyUp () {
        console.log(this.dirName)
        this.parameters['path'] = this.dirName
      },
      uploadBefore (files) {
        if (this.updir === 1) {
          this.parameters['parentObjectId'] = this.parentId
          delete this.parameters.path
        }
        this.parameters['uploadType'] = this.updir
        this.parameters['objectAcl'] = this.acl
        console.log(this.parameters)
      },
      handleUp (result) {
        if (result.code === 200) {
          let i = this.submitCount
          i++
          this.submitCount = i
          if (this.submitCount === this.fileCount) {
            this.loading = false
            this.$message({
              message: '上传成功',
              type: 'success'
            })
            this.$refs.upload.clearFiles()
            this.close()
          }
        } else {
          let i = this.submitCount
          i++
          this.submitCount = i
          if (this.submitCount === this.fileCount) {
            this.loading = false
            this.$alert(result.msg || '操作失败', '消息', {
              confirmButtonText: '确定', type: 'error'
            })
            this.$refs.upload.clearFiles()
            this.submitCount = 0
            this.fileCount = ''
          }
        }
      },
      error (err) {
        let i = this.submitCount
        i++
        this.submitCount = i
        if (this.submitCount === this.fileCount) {
          this.loading = false
          this.$alert(err.msg || '链接超时', '消息', {
            confirmButtonText: '确定', type: 'error'
          })
          this.$refs.upload.clearFiles()
          this.close()
        }
      },
      submitUpload () {
        this.loading = true
        if (this.updir === 2) {
          let reg = /^[\u4E00-\u9FA5A-Za-z0-9_\\-\\/]+$/
          if (!reg.test(this.dirName)) {
            this.$alert('目录地址不合法', '消息', {
              confirmButtonText: '确定', type: 'error'
            })
            this.loading = false
            return
          }
        }
        if (this.updir === 1) {
          this.parameters['parentObjectId'] = this.parentId
          delete this.parameters.path
        }
        this.parameters['uploadType'] = this.updir
        this.parameters['objectAcl'] = this.acl
        this.fileCount = this.$refs.upload.uploadFiles.length
        this.$refs.upload.submit()
      },
      handleRemove (file, fileList) {
        console.log(file, fileList)
      },
      handlePreview (file) {
        console.log(file)
      }
    }
  }
</script>
<style scoped>
  .panel-content {
    margin: 20px;
    box-sizing: border-box;
  }

  .el-icon-close {
    float: right;
    font-size: 12px
  }

  .upsubmit {
    left: -150px;
    position: relative;
    top: -25px;
  }

  .upload-demo {
    max-height: calc(100% - 500px);
    overflow: auto;
    min-height: 100px;
  }

  .upload-demo::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
    background-color: #F5F5F5;
  }

  .upload-demo::-webkit-scrollbar {
    width: 6px;
    background-color: #F5F5F5;
  }

  .upload-demo::-webkit-scrollbar-thumb {
    background-color: #aaa;
  }

  .side-panel {
    padding-right: 20px;
  }

  .form-row {
    margin-bottom: 16px;
    display: flex;
  }

  .form-field {
    display: inline-block;
    width: 500px;
  }

  .slide-panel-mask {
    z-index: 999;
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    background-color: rgba(55, 55, 55, .6);
    height: 100%;
  }

  .form-control {
    height: 35px;
    margin: 0;
    width: 100%;
    outline: none;
    font-weight: 400;
    vertical-align: baseline;
  }

  input.form-control {
    padding: 0 4px;
    border: 1px solid #d7d8d9;
  }

  .info {
    color: #6f7376;
    margin-top: 20px;
    line-height: 16px;
  }

  label {
    flex: 0 0 20%;
    text-align: right;
    line-height: 28px;
    font-weight: 700;
    margin-right: 10px;
  }

</style>
